package cn.itsource.org.service.impl;

import cn.itsource.org.domain.TRole;
import cn.itsource.org.mapper.RoleMapper;
import cn.itsource.org.query.RoleQuery;
import cn.itsource.org.service.IRoleService;
import cn.itsource.utils.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageList<TRole> findPageByParam(RoleQuery query) {
        PageList<TRole> pageList = new PageList<>();
        //先分析思路
        /*
            分页查询
            1. 先查询出当前这一页的数据
            2. 查询出符合条件的总条数
         */
        if (StringUtils.isNotBlank(query.getName())) {
            query.setName(query.getName().trim());
        }
        // 1. 先查询出当前这一页的数据
        List<TRole> roles = roleMapper.findPageByParam(query);
        pageList.setData(roles);

        // 2. 查询出符合条件的总条数
        Long total = roleMapper.findPageByParamCount(query);
        pageList.setTotal(total);
        return pageList;
    }
}
